//
//  SingleTableView.m
//  Muses
//
//  Created by hzyd-wlj on 14/11/11.
//  Copyright (c) 2014年 yodo. All rights reserved.
//

#import "SingleTableView.h"
#import "UIView+Positioning.h"
#import "Defines.h"
#import "UIColor+FlatUI.h"
//#import "Course.h"
//#import "AgeSectionManager.h"
//#import "SortWayManager.h"

#define HeightOfTableView 44.0f
NSString *const TableViewWillShowNotification = @"TableViewWillShowNotification";

@interface SingleTableView ()
@property (assign, readwrite, nonatomic) BOOL isOpen;
@property (assign, readwrite, nonatomic) BOOL isAnimating;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewVerticalSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraints;
@property (assign,nonatomic) SingleTableViewDataType dataType;

@end

@implementation SingleTableView

#pragma mark - View Life Cycle
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = AppFrame;
    self.backgroundView.frame = AppFrame;
    self.tableView.width = AppFrame.size.width;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.alpha = 1;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableViewWillShow:) name:TableViewWillShowNotification object:nil];
}

#pragma mark - Notification Methods
- (void)tableViewWillShow:(NSNotification *)notification
{
    if (notification.object == self) {
        return;
    }
    
    //收到通知后，给其他tableview让位。
    [self closeWithCompletion:nil withAnimated:YES];
}

#pragma mark - Actions
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(cancleTabelView)]) {
        [self.delegate cancleTabelView];
    }
    
    [self closeWithCompletion:self.closeCompletionHandler withAnimated:YES];
}

- (void)showFromPosition:(CGPoint)position inViewController:(UIViewController *)viewController dataType:(SingleTableViewDataType)dataType
{
    self.dataType = dataType;
    if (self.isOpen) {
        [self closeWithCompletion:nil withAnimated:YES];
    }
    if (self.isAnimating) {
        return;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:TableViewWillShowNotification object:self];
    self.isAnimating = YES;
    self.view.hidden = NO;
    
    [UIView animateWithDuration:0.3
                          delay:0.0
         usingSpringWithDamping:1.0
          initialSpringVelocity:4.0
                        options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.backgroundView.alpha = 0.3;
                         self.tableViewVerticalSpace.constant = position.y;
                         self.arrawImageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DEGREES_TO_RADIANS(180));
                         [self.view layoutIfNeeded];
                     } completion:^(BOOL finished) {
                         self.isAnimating = NO;
                         self.isOpen = YES;
                     }];
}

- (void)closeWithCompletion:(void (^)(void))completion withAnimated:(BOOL)isAnimated
{
    
    if (self.isAnimating) return;
    
    self.isAnimating = YES;
    
    [UIView animateWithDuration:isAnimated?0.3:0
                          delay:0.0
                        options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
                     animations:^ {
                         self.backgroundView.alpha = 0;

                         self.tableViewVerticalSpace.constant = -self.tableView.height;
                         self.arrawImageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, DEGREES_TO_RADIANS(0));
                         [self.view layoutIfNeeded];
                     } completion:^(BOOL finished) {
                         self.isOpen = NO;
                         self.isAnimating = NO;
                         
                         self.view.hidden = YES;
                         
                         if (completion) {
                             completion();
                         }
                         
                         if (self.closeCompletionHandler) {
                             self.closeCompletionHandler();
                         }
                     }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    
    [self configureCell:cell forRowAtIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title;
    if (self.dataType == SingleTableViewDataTypeAgeSection) {
//        AgeSection *ageSection = self.dataArray[indexPath.row];
        title = self.dataArray[indexPath.row];
    }else if(self.dataType == SingleTableViewDataTypeSortModel){
//        SortModel *sortModel = self.dataArray[indexPath.row];
        title = self.dataArray[indexPath.row];
    }else{
        title = self.dataArray[indexPath.row];
    }
    
    cell.textLabel.text = title;
//    cell.textLabel.highlightedTextColor = [UIColor colorFromHexCode:@"FF9B5F"];
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HeightOfTableView;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(singleTableView:didSelectedCellAtIndexPath:)]) {
        [self.delegate singleTableView:self didSelectedCellAtIndexPath:indexPath];
    }
    [self closeWithCompletion:self.closeCompletionHandler withAnimated:YES];
}

#pragma mark - Properties
- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;

    self.tableViewHeightConstraints.constant = MAX([dataArray count] * HeightOfTableView,5);
    if (self.tableViewHeightConstraints.constant > AppBounds.size.height*2/3-104) {
        self.tableViewHeightConstraints.constant = AppBounds.size.height*2/3-104;
    }
    [self.tableView reloadData];
}


@end
